import { formatDate } from '../node_modules/iview/src/components/date-picker/util';
export default {

    /**区绝对值*/
    abs(x) {
        return Math.abs(x);
    },

    /**比较两个对象或数组是否相同*/
    isObjectValueEqual(a, b) {
        if (JSON.stringify(a) == JSON.stringify(b)) {
            return true;
        } else {
            return false;
        }
    },

    /**排序*/
    changeSort(param) {
        let hasIndex = false;
        this.search.forEach((v, index) => {
            if (v.operate=='orderBy' && v.index == param.key) {
                hasIndex = true;
                this.$set(this.search, index, {index: param.key, operate: "orderBy", value: param.order});
            }
        });
        if (!hasIndex) {
            this.search.push({index: param.key, operate: "orderBy", value: param.order});
        }
    },

    /**生成搜索数据*/
    createSearch(data) {
        let search = [];
        data.forEach(v => {
            if (Array.isArray(v.value)) {
                if (v.type == 'daterange' && v.value.length === 2) {
                    let d = [];
                    v.value.forEach(val => {
                        if (val) {
                            let format = formatDate(val);
                            d.push(format);
                        }
                    });
                    if (d.length > 0) {
                        search.push({index:v.index, operate:v.operate, value:d});
                    }
                }
            } else {
                if (v.value!=undefined && v.value!='') {
                    search.push({index:v.index, operate:v.operate, value:v.value});
                }
            }
        });
        return search;
    },

    /**处理columns中render*/
    createColumns(param) {
        param.forEach((item, index) => {
            let newItem = {};
            for (let key in item) {
                if (key == 'render') {
                    switch (item[key]) {
                        case 'ellipsis':
                            newItem.render = function(h, params) {
                                return h('Tooltip', {
                                    props: { placement: 'top-start' }
                                }, [
                                    h('span', {style:{overflow: 'hidden'}}, params.row[params.column.key]),
                                    h('div', {
                                        slot: 'content',
                                        style: { whiteSpace: 'normal', wordBreak: 'break-all'}
                                    }, params.row[params.column.key])
                                ]);
                            }
                            break;

                        case 'array':
                            newItem.render = function(h, params) {
                                return params.column.data[params.row[params.column.key]] || '未定义';
                            }
                            break;

                        case 'link':
                            let _this = this;
                            newItem.render = function(h, params) {
                                return h('a', {
                                    on: {
                                        click: () => {_this.hrefTo(params)}
                                    }
                                }, params.row[params.column.key]);
                            }
                            break;

                        case 'img':
                            let imgUrl = this.$gConst.IMGHOST;
                            newItem.render = function(h, params) {
                                return h('img', {
                                    attrs : {src : imgUrl+params.row[params.column.key], style: 'width:50px;max-height:100px;margin:5px;'}
                                });
                            }
                            break;

                    }
                } else {
                    this.$set(newItem, key, item[key]);
                }

            }
            this.$set(param, index, newItem);
        });

        return param;
    },

    /**生成操作按钮*/
    makeBtn (btnArr) {
        return {
            title: '操作',
            align: 'center',
            fixed: 'right',
            width: 150,
            render: (h, params) => {
                let btn = [];
                let childBtn = [];
                btnArr.forEach(v => {
                    switch (v) {
                        case 'add' :
                            this.addBtn = true;
                            break;

                        case 'detail' :
                            btn.push(
                                h('Button', {
                                    props: {type: 'success', size: 'small'},
                                    style: {marginRight: '5px'},
                                    on: {
                                        click: () => {this.detail(params)}
                                    }
                                }, '查看')
                            );
                            break;

                        case 'edit' :
                            childBtn.push(
                                h('DropdownItem', {
                                    nativeOn: {
                                        click: () => {this.edit(params)}
                                    }
                                }, '编辑')
                            );
                            break;

                        case 'remove' :
                            childBtn.push(
                                h('DropdownItem', {
                                    nativeOn: {
                                        click: () => {this.remove(params)}
                                    }
                                }, '删除'),
                            );
                            break;

                        case 'enable' :
                            childBtn.push(
                                h('DropdownItem', {
                                    nativeOn: {
                                        click: () => {this.enable(params)}
                                    }
                                }, params.row.is_enable?'禁用':'启用'),
                            );
                            break;

                        case 'show' :
                            childBtn.push(
                                h('DropdownItem', {
                                    nativeOn: {
                                        click: () => {this.show(params)}
                                    }
                                }, params.row.is_show?'下线':'上线'),
                            );
                            break;
                    }
                });

                btn.push(
                    h('Dropdown', {props:{placement:'bottom'}}, [
                        h('Button', {
                            props: {type: 'primary', size: 'small'},
                            style: {marginRight: '5px'}
                        }, '管理'),
                        h('DropdownMenu', {slot: "list"}, childBtn),
                    ])
                );

                return h('div', btn);
            }
        }
    },

    /**列表数据表格化*/
    pageDataInit(res) {
        let d = res.data;
        if (d.code == 0) {
            if (this.title == '') {
                this.title = d.data.conf.title;
            }

            if (this.search.length == 0) {
                this.search = d.data.search;
            }

            if (this.columns.length == 0) {
                let param = this.$gFun.createColumns.call(this, d.data.columns);
                param.push(this.$gFun.makeBtn.call(this, d.data.buttons));

                this.columns = param;
            }

            this.rows = d.data.rows;
            this.pageParams.total = d.data.total;
            this.pageParams.current_page = d.data.current_page;
            this.pageParams.total_page = d.data.total_page;
        } else {
            this.$Notice.error({
                title: 'Error Warning',
                desc: `Error：${d.msg}<br>`
            });
        }
    },

    /**转化为tree的数据*/
    makeTreeData (data, title) {
        title = title || 'display_name';

        let res = [];
        data.forEach(item => {
            let child1 = [];
            item.child.forEach(val => {
                let child2 = [];
                val.child.forEach(v => {
                    child2.push({title: v[title], uuid: v.uuid});
                });

                child1.push({expand: true, title: val[title], uuid: val.uuid, children: child2});
            });

            res.push({expand: true, title: item[title], uuid: item.uuid, children: child1});
        });
        return res;
    },

    /**获取选中项及父级uuid*/
    getCkeckUuid (checkedNodes, treeData) {
        let tmpUuids = [];
        checkedNodes.map(item => {
            const parents = this.$gFun.getParentUuids.call(this, treeData, item.uuid);
            tmpUuids = tmpUuids.concat(parents);
        });

        checkedNodes.map(item => {
            tmpUuids.push(item.uuid);
        });

        let set = new Set(tmpUuids);
        return Array.from(set);//去重
    },

    /**获取某节点的所有父级uuid*/
    getParentUuids(data, uuid){
        let uuids=[];
        function loop(json) {
            for (let obj of json) {
                if (obj.children) {
                    if (JSON.stringify(obj).match(uuid)) {
                        uuids.push(obj.uuid);
                    }
                    if (obj.uuid !== uuid) {
                        loop(obj.children);
                    } else {
                        break;
                    }
                }
            }
        };
        loop(data);
        return uuids;
    }
}